import turtle
import math
import time

# 设置屏幕
screen = turtle.Screen()
screen.title("鳄鱼逃跑模拟")
screen.bgcolor("white")
screen.setup(width=800, height=800)
screen.tracer(0)

# 正方形边长（实际1000米，显示用500像素）
SQUARE_SIZE = 500
ACTUAL_SIZE = 1000  # 实际距离1000米
scale_factor = ACTUAL_SIZE / (math.sqrt(2) * SQUARE_SIZE)  # 对角线缩放因子


# 绘制正方形
def draw_square():
    t = turtle.Turtle(visible=False)
    t.speed(0)
    t.penup()
    t.goto(-SQUARE_SIZE / 2, -SQUARE_SIZE / 2)
    t.pendown()
    for _ in range(4):
        t.forward(SQUARE_SIZE)
        t.left(90)
    return t


square = draw_square()

# 设置人（中心）
person = turtle.Turtle(shape="circle", visible=True)
person.color("blue")
person.penup()
person.goto(0, 0)
person_speed = 10  # m/s

# 四条鳄鱼
crocodiles = []
positions = [
    (SQUARE_SIZE / 2, -SQUARE_SIZE / 2),  # A 右下角 (15m/s)
    (-SQUARE_SIZE / 2, -SQUARE_SIZE / 2),  # B 左下角 (20m/s)
    (-SQUARE_SIZE / 2, SQUARE_SIZE / 2),  # C 左上角 (20m/s)
    (SQUARE_SIZE / 2, SQUARE_SIZE / 2)  # D 右上角 (20m/s)
]
speeds = [15, 20, 20, 20]
colors = ["red", "green", "orange", "purple"]

for i, pos in enumerate(positions):
    c = turtle.Turtle(shape="circle", visible=True)
    c.color(colors[i])
    c.penup()
    c.goto(pos)
    c.speed = speeds[i]
    crocodiles.append(c)


# 计算实际距离（考虑缩放）
def actual_distance(t1, t2):
    dx = (t1.xcor() - t2.xcor()) * scale_factor
    dy = (t1.ycor() - t2.ycor()) * scale_factor
    return math.sqrt(dx * dx + dy * dy)


# 找到最近鳄鱼
def nearest_crocodile():
    min_dist = float('inf')
    nearest = None
    for c in crocodiles:
        dist = actual_distance(person, c)
        if dist < min_dist:
            min_dist = dist
            nearest = c
    return nearest, min_dist


# 逃跑策略（考虑实际距离）
def escape(time_step):
    nearest, dist = nearest_crocodile()
    if dist == 0: return

    # 计算实际移动方向（远离最近鳄鱼）
    dx = (person.xcor() - nearest.xcor()) * scale_factor
    dy = (person.ycor() - nearest.ycor()) * scale_factor
    norm = math.sqrt(dx * dx + dy * dy)

    # 计算新位置（考虑缩放）
    move_x = (dx / norm) * person_speed * time_step / scale_factor
    move_y = (dy / norm) * person_speed * time_step / scale_factor
    person.goto(person.xcor() + move_x, person.ycor() + move_y)


# 鳄鱼移动（考虑实际距离）
def move_crocodiles(time_step):
    for c in crocodiles:
        dx = (person.xcor() - c.xcor()) * scale_factor
        dy = (person.ycor() - c.ycor()) * scale_factor
        norm = math.sqrt(dx * dx + dy * dy)

        move_x = (dx / norm) * c.speed * time_step / scale_factor
        move_y = (dy / norm) * c.speed * time_step / scale_factor
        c.goto(c.xcor() + move_x, c.ycor() + move_y)


# 主循环
start_time = time.time()
time_step = 0.05  # 更小的时间步长提高精度
safe_distance = 1  # 1米判定为被捕获

while True:
    screen.update()

    # 检查是否被捕获
    nearest, dist = nearest_crocodile()
    if dist <= safe_distance:
        elapsed = time.time() - start_time
        print(f"存活时间: {elapsed:.2f}秒")
        break

    # 人和鳄鱼移动
    escape(time_step)
    move_crocodiles(time_step)

    time.sleep(time_step / 10)

screen.mainloop()